package qiurank;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.Map;

/**
 * Created by Administrator on 2015/12/31.
 */
public class Http {
    public static String cookie=null;
    public static String get(String url,String opt,String opt_val){
        try{
            URL _url=new URL(url);
            HttpURLConnection conn=(HttpURLConnection)_url.openConnection();
            if(cookie!=null){
                conn.setRequestProperty("Cookie", cookie);
            }
            if(opt!=null&&opt_val!=null){
                conn.setRequestProperty(opt, opt_val);
            }
            conn.setRequestProperty("User-Agent","Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.152 Safari/537.36");
            conn.connect();
            if(conn.getResponseCode()==200) {
                InputStream is = conn.getInputStream();
                BufferedReader bf = new BufferedReader(new InputStreamReader(is));
                String line = null;
                StringBuffer sb = new StringBuffer();
                sb.append("<jphtml>");
                while ((line = bf.readLine()) != null) {
                    sb.append(line);
                }
                sb.append("</jphtml>");
                sb.append("<Content-Type>" + conn.getHeaderField("Content-Type")+"</Content-Type>");
                if(conn.getHeaderField("Set-Cookie")!=null) {
                    cookie = conn.getHeaderField("Set-Cookie");
                }
                return sb.toString();
            }else {
                Log.show("请求URL:" + url + "页面发生错误.HTTP ERROR CODE:" + conn.getResponseCode());
                return "fail";
            }
        }catch (Exception ex){
            Log.show("请求URL:"+url+"发生异常.错误:"+ex.toString());
        }
        return "fail";
    }
    public static String get(String url){
        return get(url,null,null);
    }

    public static String post(String url,Map<String,Object> map){
        try{
            URL _url=new URL(url);
            HttpURLConnection conn=(HttpURLConnection)_url.openConnection();
            conn.setDoInput(true);
            conn.setDoOutput(true);
            conn.setRequestMethod("POST");
            conn.setReadTimeout(5000);
            conn.setConnectTimeout(5000);
            conn.setRequestProperty("Connection", "keep-alive");
            conn.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
            conn.setUseCaches(false);
            String data="";
            for(Map.Entry<String,Object> entry:map.entrySet()){
                data+= URLEncoder.encode("" + entry.getKey(), "UTF-8")+ "=" + URLEncoder.encode("" + entry.getValue(), "UTF-8") + "&";
            }

            conn.setRequestProperty("Content-Length",String.valueOf(data.getBytes().length));
            conn.setRequestProperty("User-Agent","Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.152 Safari/537.36");
            conn.connect();
            OutputStream os=conn.getOutputStream();
            DataOutputStream dos=new DataOutputStream(os);
            dos.write(data.getBytes());
            dos.close();
            if(conn.getResponseCode()==200) {
                InputStream is = conn.getInputStream();
                BufferedReader bf = new BufferedReader(new InputStreamReader(is));
                String line = null;
                StringBuffer sb = new StringBuffer();
                sb.append("<jphtml>");
                while ((line = bf.readLine()) != null) {
                    sb.append(line);
                }
                sb.append("</jphtml>");
                sb.append("<Content-Type>" + conn.getHeaderField("Content-Type")+"</Content-Type>");
                return sb.toString();
            }else{
                Log.show("URL:" + url + ",data:" + data + ",请求页面发生错误,HTTP ERROR CODE:" + conn.getResponseCode());
                return "fail";
            }
        }catch (Exception ex){

            Log.show("请求URL:" + url + "发生异常.错误:" + ex.toString());
        }
        return "fail";
    }
}
